package com.ridaizu.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.google.common.base.Strings;
import com.ridaizu.cache.CacheDataBean;
import com.ridaizu.dao.db.DBManager;
import com.ridaizu.lazy.LazyBrandSeriesManagerDataModel;
import com.ridaizu.model.BrandSeriesManager;
import com.ridaizu.model.form.BrandSeriesManagerForm;
import com.ridaizu.model.search.BrandSeriesManagerSearch;
import com.ridaizu.service.impl.BrandSeriesManagerServiceImpl;
import com.ridaizu.util.DateUtil;

@javax.faces.bean.ManagedBean(name = "brandSeriesManagerBean")
@SessionScoped
public class BrandSeriesManagerBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private LazyDataModel<BrandSeriesManager> lazyModel;

    @ManagedProperty("#{cacheDataBean}")
    private CacheDataBean cacheDataBean;

    @ManagedProperty("#{brandSeriesManagerServiceImpl}")
    private BrandSeriesManagerServiceImpl brandSeriesManagerService;

    @ManagedProperty("#{brandSeriesManagerSearch}")
    private BrandSeriesManagerSearch brandSeriesManagerSearch;

    @ManagedProperty("#{brandSeriesManagerForm}")
    private BrandSeriesManagerForm brandSeriesManagerForm;

	public void searchBrandSeriesManagerSync() {
		lazyModel = new LazyBrandSeriesManagerDataModel(brandSeriesManagerSearch);
	}

	public void viewRegisterBrandSeriesManager() throws SQLException {

		setInitValues(null);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 500);

        RequestContext.getCurrentInstance().openDialog("brand-series-manager", options, null);
    }

	public void viewUpdateBrandSeriesManager(BrandSeriesManager brandSeriesManager) throws SQLException {

		setInitValues(brandSeriesManager);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 520);

        RequestContext.getCurrentInstance().openDialog("brand-series-manager", options, null);
    }

	private void setInitValues(BrandSeriesManager brandSeriesManager) throws SQLException {
		brandSeriesManagerForm.clearAll();
		setValues(brandSeriesManager);
	}

	private void setValues(BrandSeriesManager brandSeriesManager) {
		if (brandSeriesManager == null) {
			setRegisterValues();
		} else {
			setUpdateValues(brandSeriesManager);
		}
	}

	private void setRegisterValues() {
	}

	private void setUpdateValues(BrandSeriesManager brandSeriesManager) {
		brandSeriesManagerForm.setId(String.valueOf(brandSeriesManager.getId()));
		brandSeriesManagerForm.setBrandId(String.valueOf(brandSeriesManager.getBrandId()));
		brandSeriesManagerForm.setBrandSeriesId(String.valueOf(brandSeriesManager.getBrandSeriesId()));
		brandSeriesManagerForm.setRgtOpt(brandSeriesManager.getRgtOpt());
		brandSeriesManagerForm.setRgtDate(new Date(brandSeriesManager.getRgtDate().getTime()));
		brandSeriesManagerForm.setUpdOpt(brandSeriesManager.getUpdOpt());
		brandSeriesManagerForm.setUpdDate(new Date(brandSeriesManager.getUpdDate().getTime()));
	}

	public void registerBrandSeriesManager() {
		BrandSeriesManager brandSeriesManager = getBrandSeriesManager();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = brandSeriesManagerService.insertModel(brandSeriesManager);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
    }

	public void updateBrandSeriesManager() {

		BrandSeriesManager brandSeriesManager = getBrandSeriesManager();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = brandSeriesManagerService.updateModel(brandSeriesManager);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
	}

	public void deleteBrandSeriesManager() {

		BrandSeriesManager selectedBrandSeriesManager = brandSeriesManagerForm.getSelectedBrandSeriesManager() ;

		if (selectedBrandSeriesManager == null || selectedBrandSeriesManager.getId() < 1) {
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "请选择要删除的品牌系列管理信息。");
	        FacesContext.getCurrentInstance().addMessage(null, message);

	        return ;
		}

		boolean result = false;
		try {
			DBManager.beginTransaction();
			result = brandSeriesManagerService.deleteModel(selectedBrandSeriesManager.getId());
			DBManager.commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		String resultStr = null;
		if (result) {
			resultStr = String.format("品牌系列管理[%s]删除成功。", selectedBrandSeriesManager.getId());
		} else {
			resultStr = String.format("品牌系列管理[%s]删除失败。", selectedBrandSeriesManager.getId());
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "BrandSeriesManager delete result", resultStr);
        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void registerBrandSeriesManagerResult(SelectEvent event) {
		boolean result = (Boolean) event.getObject();

		handleResult(result, "登录");
	}

	public void updateBrandSeriesManagerResult(SelectEvent event) {

		boolean result = (Boolean) event.getObject();

		handleResult(result, "更新");
	}

	private void handleResult(boolean result, String optStr) {

		String resultStr = null;
		if (result) {
			resultStr = String.format("品牌系列管理信息%s成功。", optStr);
		} else {
			resultStr = String.format("品牌系列管理信息%s失败。", optStr);;
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", resultStr);

        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private BrandSeriesManager getBrandSeriesManager() {

		BrandSeriesManager brandSeriesManager = new BrandSeriesManager();

		if (!Strings.isNullOrEmpty(brandSeriesManagerForm.getId())) {
			brandSeriesManager.setId(Integer.valueOf(brandSeriesManagerForm.getId()));
		}

		if (!Strings.isNullOrEmpty(brandSeriesManagerForm.getBrandId())) {
			brandSeriesManager.setBrandId(Integer.valueOf(brandSeriesManagerForm.getBrandId()));
		}

		if (!Strings.isNullOrEmpty(brandSeriesManagerForm.getBrandSeriesId())) {
			brandSeriesManager.setBrandSeriesId(Integer.valueOf(brandSeriesManagerForm.getBrandSeriesId()));
		}

		Timestamp currentDate = DateUtil.getCurrentDateTime();

		brandSeriesManager.setRgtOpt("Admin");
		brandSeriesManager.setRgtDate(currentDate);
		brandSeriesManager.setUpdOpt("Admin");
		brandSeriesManager.setUpdDate(currentDate);

		return brandSeriesManager;
	}

	public LazyDataModel<BrandSeriesManager> getLazyModel() {
		return lazyModel;
	}

	public void setLazyModel(LazyDataModel<BrandSeriesManager> lazyModel) {
		this.lazyModel = lazyModel;
	}

	public BrandSeriesManagerServiceImpl getBrandSeriesManagerService() {
		return brandSeriesManagerService;
	}

	public void setBrandSeriesManagerService(BrandSeriesManagerServiceImpl brandSeriesManagerService) {
		this.brandSeriesManagerService = brandSeriesManagerService;
	}

	public BrandSeriesManagerSearch getBrandSeriesManagerSearch() {
		return brandSeriesManagerSearch;
	}

	public void setBrandSeriesManagerSearch(BrandSeriesManagerSearch brandSeriesManagerSearch) {
		this.brandSeriesManagerSearch = brandSeriesManagerSearch;
	}

	public BrandSeriesManagerForm getBrandSeriesManagerForm() {
		return brandSeriesManagerForm;
	}

	public void setBrandSeriesManagerForm(BrandSeriesManagerForm brandSeriesManagerForm) {
		this.brandSeriesManagerForm = brandSeriesManagerForm;
	}

	public CacheDataBean getCacheDataBean() {
		return cacheDataBean;
	}

	public void setCacheDataBean(CacheDataBean cacheDataBean) {
		this.cacheDataBean = cacheDataBean;
	}

}